/
loops.c.src
2936 lines (2670 loc) · 84.3 KB
/
loops.c.src
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- c -*- */
#define _UMATHMODULE
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#include "Python.h"
#include "npy_config.h"
#define PY_ARRAY_UNIQUE_SYMBOL _npy_umathmodule_ARRAY_API
#define NO_IMPORT_ARRAY
#include "numpy/npy_common.h"
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
#include "numpy/npy_math.h"
#include "numpy/halffloat.h"
#include "lowlevel_strided_loops.h"
#include "npy_pycompat.h"
#include "ufunc_object.h"
#include <string.h> /* for memchr */
/*
* cutoff blocksize for pairwise summation
* decreasing it decreases errors slightly as more pairs are summed but
* also lowers performance, as the inner loop is unrolled eight times it is
* effectively 16
*/
#define PW_BLOCKSIZE 128
/*
* largest simd vector size in bytes numpy supports
* it is currently a extremely large value as it is only used for memory
* overlap checks
*/
#ifndef NPY_MAX_SIMD_SIZE
#define NPY_MAX_SIMD_SIZE 1024
#endif
/*
* include vectorized functions and dispatchers
* this file is safe to include also for generic builds
* platform specific instructions are either masked via the proprocessor or
* runtime detected
*/
#include "simd.inc"
/*
*****************************************************************************
** UFUNC LOOPS **
*****************************************************************************
*/
/* unary loop input and output contiguous */
#define IS_UNARY_CONT(tin, tout) (steps[0] == sizeof(tin) && \
steps[1] == sizeof(tout))
#define IS_BINARY_REDUCE ((args[0] == args[2])\
&& (steps[0] == steps[2])\
&& (steps[0] == 0))
/* binary loop input and output contiguous */
#define IS_BINARY_CONT(tin, tout) (steps[0] == sizeof(tin) && \
steps[1] == sizeof(tin) && \
steps[2] == sizeof(tout))
/* binary loop input and output contiguous with first scalar */
#define IS_BINARY_CONT_S1(tin, tout) (steps[0] == 0 && \
steps[1] == sizeof(tin) && \
steps[2] == sizeof(tout))
/* binary loop input and output contiguous with second scalar */
#define IS_BINARY_CONT_S2(tin, tout) (steps[0] == sizeof(tin) && \
steps[1] == 0 && \
steps[2] == sizeof(tout))
#define OUTPUT_LOOP\
char *op1 = args[1];\
npy_intp os1 = steps[1];\
npy_intp n = dimensions[0];\
npy_intp i;\
for(i = 0; i < n; i++, op1 += os1)
#define UNARY_LOOP\
char *ip1 = args[0], *op1 = args[1];\
npy_intp is1 = steps[0], os1 = steps[1];\
npy_intp n = dimensions[0];\
npy_intp i;\
for(i = 0; i < n; i++, ip1 += is1, op1 += os1)
/*
* loop with contiguous specialization
* op should be the code working on `tin in` and
* storing the result in `tout * out`
* combine with NPY_GCC_OPT_3 to allow autovectorization
* should only be used where its worthwhile to avoid code bloat
*/
#define BASE_UNARY_LOOP(tin, tout, op) \
UNARY_LOOP { \
const tin in = *(tin *)ip1; \
tout * out = (tout *)op1; \
op; \
}
#define UNARY_LOOP_FAST(tin, tout, op) \
do { \
/* condition allows compiler to optimize the generic macro */ \
if (IS_UNARY_CONT(tin, tout)) { \
if (args[0] == args[1]) { \
BASE_UNARY_LOOP(tin, tout, op) \
} \
else { \
BASE_UNARY_LOOP(tin, tout, op) \
} \
} \
else { \
BASE_UNARY_LOOP(tin, tout, op) \
} \
} \
while (0)
#define UNARY_LOOP_TWO_OUT\
char *ip1 = args[0], *op1 = args[1], *op2 = args[2];\
npy_intp is1 = steps[0], os1 = steps[1], os2 = steps[2];\
npy_intp n = dimensions[0];\
npy_intp i;\
for(i = 0; i < n; i++, ip1 += is1, op1 += os1, op2 += os2)
#define BINARY_LOOP\
char *ip1 = args[0], *ip2 = args[1], *op1 = args[2];\
npy_intp is1 = steps[0], is2 = steps[1], os1 = steps[2];\
npy_intp n = dimensions[0];\
npy_intp i;\
for(i = 0; i < n; i++, ip1 += is1, ip2 += is2, op1 += os1)
/*
* loop with contiguous specialization
* op should be the code working on `tin in1`, `tin in2` and
* storing the result in `tout * out`
* combine with NPY_GCC_OPT_3 to allow autovectorization
* should only be used where its worthwhile to avoid code bloat
*/
#define BASE_BINARY_LOOP(tin, tout, op) \
BINARY_LOOP { \
const tin in1 = *(tin *)ip1; \
const tin in2 = *(tin *)ip2; \
tout * out = (tout *)op1; \
op; \
}
/*
* unfortunately gcc 6/7 regressed and we need to give it additional hints to
* vectorize inplace operations (PR80198)
* must only be used after op1 == ip1 or ip2 has been checked
* TODO: using ivdep might allow other compilers to vectorize too
*/
#if __GNUC__ >= 6
#define IVDEP_LOOP _Pragma("GCC ivdep")
#else
#define IVDEP_LOOP
#endif
#define BASE_BINARY_LOOP_INP(tin, tout, op) \
char *ip1 = args[0], *ip2 = args[1], *op1 = args[2];\
npy_intp is1 = steps[0], is2 = steps[1], os1 = steps[2];\
npy_intp n = dimensions[0];\
npy_intp i;\
IVDEP_LOOP \
for(i = 0; i < n; i++, ip1 += is1, ip2 += is2, op1 += os1) { \
const tin in1 = *(tin *)ip1; \
const tin in2 = *(tin *)ip2; \
tout * out = (tout *)op1; \
op; \
}
#define BASE_BINARY_LOOP_S(tin, tout, cin, cinp, vin, vinp, op) \
const tin cin = *(tin *)cinp; \
BINARY_LOOP { \
const tin vin = *(tin *)vinp; \
tout * out = (tout *)op1; \
op; \
}
/* PR80198 again, scalar works without the pragma */
#define BASE_BINARY_LOOP_S_INP(tin, tout, cin, cinp, vin, vinp, op) \
const tin cin = *(tin *)cinp; \
BINARY_LOOP { \
const tin vin = *(tin *)vinp; \
tout * out = (tout *)vinp; \
op; \
}
#define BINARY_LOOP_FAST(tin, tout, op) \
do { \
/* condition allows compiler to optimize the generic macro */ \
if (IS_BINARY_CONT(tin, tout)) { \
if (abs_ptrdiff(args[2], args[0]) == 0 && \
abs_ptrdiff(args[2], args[1]) >= NPY_MAX_SIMD_SIZE) { \
BASE_BINARY_LOOP_INP(tin, tout, op) \
} \
else if (abs_ptrdiff(args[2], args[1]) == 0 && \
abs_ptrdiff(args[2], args[0]) >= NPY_MAX_SIMD_SIZE) { \
BASE_BINARY_LOOP_INP(tin, tout, op) \
} \
else { \
BASE_BINARY_LOOP(tin, tout, op) \
} \
} \
else if (IS_BINARY_CONT_S1(tin, tout)) { \
if (abs_ptrdiff(args[2], args[1]) == 0) { \
BASE_BINARY_LOOP_S_INP(tin, tout, in1, args[0], in2, ip2, op) \
} \
else { \
BASE_BINARY_LOOP_S(tin, tout, in1, args[0], in2, ip2, op) \
} \
} \
else if (IS_BINARY_CONT_S2(tin, tout)) { \
if (abs_ptrdiff(args[2], args[0]) == 0) { \
BASE_BINARY_LOOP_S_INP(tin, tout, in2, args[1], in1, ip1, op) \
} \
else { \
BASE_BINARY_LOOP_S(tin, tout, in2, args[1], in1, ip1, op) \
}\
} \
else { \
BASE_BINARY_LOOP(tin, tout, op) \
} \
} \
while (0)
#define BINARY_REDUCE_LOOP_INNER\
char *ip2 = args[1]; \
npy_intp is2 = steps[1]; \
npy_intp n = dimensions[0]; \
npy_intp i; \
for(i = 0; i < n; i++, ip2 += is2)
#define BINARY_REDUCE_LOOP(TYPE)\
char *iop1 = args[0]; \
TYPE io1 = *(TYPE *)iop1; \
BINARY_REDUCE_LOOP_INNER
#define BINARY_LOOP_TWO_OUT\
char *ip1 = args[0], *ip2 = args[1], *op1 = args[2], *op2 = args[3];\
npy_intp is1 = steps[0], is2 = steps[1], os1 = steps[2], os2 = steps[3];\
npy_intp n = dimensions[0];\
npy_intp i;\
for(i = 0; i < n; i++, ip1 += is1, ip2 += is2, op1 += os1, op2 += os2)
/******************************************************************************
** GENERIC FLOAT LOOPS **
*****************************************************************************/
typedef float halfUnaryFunc(npy_half x);
typedef float floatUnaryFunc(float x);
typedef double doubleUnaryFunc(double x);
typedef npy_longdouble longdoubleUnaryFunc(npy_longdouble x);
typedef npy_half halfBinaryFunc(npy_half x, npy_half y);
typedef float floatBinaryFunc(float x, float y);
typedef double doubleBinaryFunc(double x, double y);
typedef npy_longdouble longdoubleBinaryFunc(npy_longdouble x, npy_longdouble y);
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_e_e(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
halfUnaryFunc *f = (halfUnaryFunc *)func;
UNARY_LOOP {
const npy_half in1 = *(npy_half *)ip1;
*(npy_half *)op1 = f(in1);
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_e_e_As_f_f(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
floatUnaryFunc *f = (floatUnaryFunc *)func;
UNARY_LOOP {
const float in1 = npy_half_to_float(*(npy_half *)ip1);
*(npy_half *)op1 = npy_float_to_half(f(in1));
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_e_e_As_d_d(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
doubleUnaryFunc *f = (doubleUnaryFunc *)func;
UNARY_LOOP {
const double in1 = npy_half_to_double(*(npy_half *)ip1);
*(npy_half *)op1 = npy_double_to_half(f(in1));
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_f_f(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
floatUnaryFunc *f = (floatUnaryFunc *)func;
UNARY_LOOP {
const float in1 = *(float *)ip1;
*(float *)op1 = f(in1);
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_f_f_As_d_d(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
doubleUnaryFunc *f = (doubleUnaryFunc *)func;
UNARY_LOOP {
const float in1 = *(float *)ip1;
*(float *)op1 = (float)f((double)in1);
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_ee_e(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
halfBinaryFunc *f = (halfBinaryFunc *)func;
BINARY_LOOP {
npy_half in1 = *(npy_half *)ip1;
npy_half in2 = *(npy_half *)ip2;
*(npy_half *)op1 = f(in1, in2);
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_ee_e_As_ff_f(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
floatBinaryFunc *f = (floatBinaryFunc *)func;
BINARY_LOOP {
float in1 = npy_half_to_float(*(npy_half *)ip1);
float in2 = npy_half_to_float(*(npy_half *)ip2);
*(npy_half *)op1 = npy_float_to_half(f(in1, in2));
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_ee_e_As_dd_d(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
doubleBinaryFunc *f = (doubleBinaryFunc *)func;
BINARY_LOOP {
double in1 = npy_half_to_double(*(npy_half *)ip1);
double in2 = npy_half_to_double(*(npy_half *)ip2);
*(npy_half *)op1 = npy_double_to_half(f(in1, in2));
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_ff_f(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
floatBinaryFunc *f = (floatBinaryFunc *)func;
BINARY_LOOP {
float in1 = *(float *)ip1;
float in2 = *(float *)ip2;
*(float *)op1 = f(in1, in2);
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_ff_f_As_dd_d(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
doubleBinaryFunc *f = (doubleBinaryFunc *)func;
BINARY_LOOP {
float in1 = *(float *)ip1;
float in2 = *(float *)ip2;
*(float *)op1 = (double)f((double)in1, (double)in2);
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_d_d(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
doubleUnaryFunc *f = (doubleUnaryFunc *)func;
UNARY_LOOP {
double in1 = *(double *)ip1;
*(double *)op1 = f(in1);
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_dd_d(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
doubleBinaryFunc *f = (doubleBinaryFunc *)func;
BINARY_LOOP {
double in1 = *(double *)ip1;
double in2 = *(double *)ip2;
*(double *)op1 = f(in1, in2);
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_g_g(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
longdoubleUnaryFunc *f = (longdoubleUnaryFunc *)func;
UNARY_LOOP {
npy_longdouble in1 = *(npy_longdouble *)ip1;
*(npy_longdouble *)op1 = f(in1);
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_gg_g(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
longdoubleBinaryFunc *f = (longdoubleBinaryFunc *)func;
BINARY_LOOP {
npy_longdouble in1 = *(npy_longdouble *)ip1;
npy_longdouble in2 = *(npy_longdouble *)ip2;
*(npy_longdouble *)op1 = f(in1, in2);
}
}
/******************************************************************************
** GENERIC COMPLEX LOOPS **
*****************************************************************************/
typedef void cdoubleUnaryFunc(npy_cdouble *x, npy_cdouble *r);
typedef void cfloatUnaryFunc(npy_cfloat *x, npy_cfloat *r);
typedef void clongdoubleUnaryFunc(npy_clongdouble *x, npy_clongdouble *r);
typedef void cdoubleBinaryFunc(npy_cdouble *x, npy_cdouble *y, npy_cdouble *r);
typedef void cfloatBinaryFunc(npy_cfloat *x, npy_cfloat *y, npy_cfloat *r);
typedef void clongdoubleBinaryFunc(npy_clongdouble *x, npy_clongdouble *y,
npy_clongdouble *r);
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_F_F(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
cfloatUnaryFunc *f = (cfloatUnaryFunc *)func;
UNARY_LOOP {
npy_cfloat in1 = *(npy_cfloat *)ip1;
npy_cfloat *out = (npy_cfloat *)op1;
f(&in1, out);
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_F_F_As_D_D(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
cdoubleUnaryFunc *f = (cdoubleUnaryFunc *)func;
UNARY_LOOP {
npy_cdouble tmp, out;
tmp.real = (double)((float *)ip1)[0];
tmp.imag = (double)((float *)ip1)[1];
f(&tmp, &out);
((float *)op1)[0] = (float)out.real;
((float *)op1)[1] = (float)out.imag;
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_FF_F(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
cfloatBinaryFunc *f = (cfloatBinaryFunc *)func;
BINARY_LOOP {
npy_cfloat in1 = *(npy_cfloat *)ip1;
npy_cfloat in2 = *(npy_cfloat *)ip2;
npy_cfloat *out = (npy_cfloat *)op1;
f(&in1, &in2, out);
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_FF_F_As_DD_D(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
cdoubleBinaryFunc *f = (cdoubleBinaryFunc *)func;
BINARY_LOOP {
npy_cdouble tmp1, tmp2, out;
tmp1.real = (double)((float *)ip1)[0];
tmp1.imag = (double)((float *)ip1)[1];
tmp2.real = (double)((float *)ip2)[0];
tmp2.imag = (double)((float *)ip2)[1];
f(&tmp1, &tmp2, &out);
((float *)op1)[0] = (float)out.real;
((float *)op1)[1] = (float)out.imag;
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_D_D(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
cdoubleUnaryFunc *f = (cdoubleUnaryFunc *)func;
UNARY_LOOP {
npy_cdouble in1 = *(npy_cdouble *)ip1;
npy_cdouble *out = (npy_cdouble *)op1;
f(&in1, out);
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_DD_D(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
cdoubleBinaryFunc *f = (cdoubleBinaryFunc *)func;
BINARY_LOOP {
npy_cdouble in1 = *(npy_cdouble *)ip1;
npy_cdouble in2 = *(npy_cdouble *)ip2;
npy_cdouble *out = (npy_cdouble *)op1;
f(&in1, &in2, out);
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_G_G(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
clongdoubleUnaryFunc *f = (clongdoubleUnaryFunc *)func;
UNARY_LOOP {
npy_clongdouble in1 = *(npy_clongdouble *)ip1;
npy_clongdouble *out = (npy_clongdouble *)op1;
f(&in1, out);
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_GG_G(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
clongdoubleBinaryFunc *f = (clongdoubleBinaryFunc *)func;
BINARY_LOOP {
npy_clongdouble in1 = *(npy_clongdouble *)ip1;
npy_clongdouble in2 = *(npy_clongdouble *)ip2;
npy_clongdouble *out = (npy_clongdouble *)op1;
f(&in1, &in2, out);
}
}
/******************************************************************************
** GENERIC OBJECT lOOPS **
*****************************************************************************/
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_O_O(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
unaryfunc f = (unaryfunc)func;
UNARY_LOOP {
PyObject *in1 = *(PyObject **)ip1;
PyObject **out = (PyObject **)op1;
PyObject *ret = f(in1 ? in1 : Py_None);
if (ret == NULL) {
return;
}
Py_XDECREF(*out);
*out = ret;
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_O_O_method(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
char *meth = (char *)func;
UNARY_LOOP {
PyObject *in1 = *(PyObject **)ip1;
PyObject **out = (PyObject **)op1;
PyObject *ret = PyObject_CallMethod(in1 ? in1 : Py_None, meth, NULL);
if (ret == NULL) {
return;
}
Py_XDECREF(*out);
*out = ret;
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_OO_O(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
binaryfunc f = (binaryfunc)func;
BINARY_LOOP {
PyObject *in1 = *(PyObject **)ip1;
PyObject *in2 = *(PyObject **)ip2;
PyObject **out = (PyObject **)op1;
PyObject *ret = f(in1 ? in1 : Py_None, in2 ? in2 : Py_None);
if (ret == NULL) {
return;
}
Py_XDECREF(*out);
*out = ret;
}
}
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_OO_O_method(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
char *meth = (char *)func;
BINARY_LOOP {
PyObject *in1 = *(PyObject **)ip1;
PyObject *in2 = *(PyObject **)ip2;
PyObject **out = (PyObject **)op1;
PyObject *ret = PyObject_CallMethod(in1 ? in1 : Py_None,
meth, "(O)", in2);
if (ret == NULL) {
return;
}
Py_XDECREF(*out);
*out = ret;
}
}
/*
* A general-purpose ufunc that deals with general-purpose Python callable.
* func is a structure with nin, nout, and a Python callable function
*/
/*UFUNC_API*/
NPY_NO_EXPORT void
PyUFunc_On_Om(char **args, npy_intp *dimensions, npy_intp *steps, void *func)
{
npy_intp n = dimensions[0];
PyUFunc_PyFuncData *data = (PyUFunc_PyFuncData *)func;
int nin = data->nin;
int nout = data->nout;
PyObject *tocall = data->callable;
char *ptrs[NPY_MAXARGS];
PyObject *arglist, *result;
PyObject *in, **op;
npy_intp i, j, ntot;
ntot = nin+nout;
for(j = 0; j < ntot; j++) {
ptrs[j] = args[j];
}
for(i = 0; i < n; i++) {
arglist = PyTuple_New(nin);
if (arglist == NULL) {
return;
}
for(j = 0; j < nin; j++) {
in = *((PyObject **)ptrs[j]);
if (in == NULL) {
in = Py_None;
}
PyTuple_SET_ITEM(arglist, j, in);
Py_INCREF(in);
}
result = PyEval_CallObject(tocall, arglist);
Py_DECREF(arglist);
if (result == NULL) {
return;
}
if (nout == 0 && result == Py_None) {
/* No output expected, no output received, continue */
Py_DECREF(result);
}
else if (nout == 1) {
/* Single output expected, assign and continue */
op = (PyObject **)ptrs[nin];
Py_XDECREF(*op);
*op = result;
}
else if (PyTuple_Check(result) && nout == PyTuple_Size(result)) {
/*
* Multiple returns match expected number of outputs, assign
* and continue. Will also gobble empty tuples if nout == 0.
*/
for(j = 0; j < nout; j++) {
op = (PyObject **)ptrs[j+nin];
Py_XDECREF(*op);
*op = PyTuple_GET_ITEM(result, j);
Py_INCREF(*op);
}
Py_DECREF(result);
}
else {
/* Mismatch between returns and expected outputs, exit */
Py_DECREF(result);
return;
}
for(j = 0; j < ntot; j++) {
ptrs[j] += steps[j];
}
}
}
/*
*****************************************************************************
** BOOLEAN LOOPS **
*****************************************************************************
*/
/**begin repeat
* #kind = equal, not_equal, greater, greater_equal, less, less_equal#
* #OP = ==, !=, >, >=, <, <=#
**/
NPY_NO_EXPORT void
BOOL_@kind@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
BINARY_LOOP {
npy_bool in1 = *((npy_bool *)ip1) != 0;
npy_bool in2 = *((npy_bool *)ip2) != 0;
*((npy_bool *)op1)= in1 @OP@ in2;
}
}
/**end repeat**/
/**begin repeat
* #kind = logical_and, logical_or#
* #OP = &&, ||#
* #SC = ==, !=#
* #and = 1, 0#
**/
NPY_NO_EXPORT void
BOOL_@kind@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
if(IS_BINARY_REDUCE) {
#ifdef NPY_HAVE_SSE2_INTRINSICS
/*
* stick with our variant for more reliable performance, only known
* platform which outperforms it by ~20% is an i7 with glibc 2.17
*/
if (run_reduce_simd_@kind@_BOOL(args, dimensions, steps)) {
return;
}
#else
/* for now only use libc on 32-bit/non-x86 */
if (steps[1] == 1) {
npy_bool * op = (npy_bool *)args[0];
#if @and@
/* np.all(), search for a zero (false) */
if (*op) {
*op = memchr(args[1], 0, dimensions[0]) == NULL;
}
#else
/*
* np.any(), search for a non-zero (true) via comparing against
* zero blocks, memcmp is faster than memchr on SSE4 machines
* with glibc >= 2.12 and memchr can only check for equal 1
*/
static const npy_bool zero[4096]; /* zero by C standard */
npy_uintp i, n = dimensions[0];
for (i = 0; !*op && i < n - (n % sizeof(zero)); i += sizeof(zero)) {
*op = memcmp(&args[1][i], zero, sizeof(zero)) != 0;
}
if (!*op && n - i > 0) {
*op = memcmp(&args[1][i], zero, n - i) != 0;
}
#endif
return;
}
#endif
else {
BINARY_REDUCE_LOOP(npy_bool) {
const npy_bool in2 = *(npy_bool *)ip2;
io1 = io1 @OP@ in2;
if (io1 @SC@ 0) {
break;
}
}
*((npy_bool *)iop1) = io1;
}
}
else {
if (run_binary_simd_@kind@_BOOL(args, dimensions, steps)) {
return;
}
else {
BINARY_LOOP {
const npy_bool in1 = *(npy_bool *)ip1;
const npy_bool in2 = *(npy_bool *)ip2;
*((npy_bool *)op1) = in1 @OP@ in2;
}
}
}
}
/**end repeat**/
/**begin repeat
* #kind = absolute, logical_not#
* #OP = !=, ==#
**/
NPY_NO_EXPORT void
BOOL_@kind@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
if (run_unary_simd_@kind@_BOOL(args, dimensions, steps)) {
return;
}
else {
UNARY_LOOP {
npy_bool in1 = *(npy_bool *)ip1;
*((npy_bool *)op1) = in1 @OP@ 0;
}
}
}
/**end repeat**/
NPY_NO_EXPORT void
BOOL__ones_like(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(data))
{
OUTPUT_LOOP {
*((npy_bool *)op1) = 1;
}
}
/*
*****************************************************************************
** INTEGER LOOPS
*****************************************************************************
*/
/**begin repeat
* #TYPE = BYTE, UBYTE, SHORT, USHORT, INT, UINT,
* LONG, ULONG, LONGLONG, ULONGLONG#
* #type = npy_byte, npy_ubyte, npy_short, npy_ushort, npy_int, npy_uint,
* npy_long, npy_ulong, npy_longlong, npy_ulonglong#
* #ftype = npy_float, npy_float, npy_float, npy_float, npy_double, npy_double,
* npy_double, npy_double, npy_double, npy_double#
* #SIGNED = 1, 0, 1, 0, 1, 0, 1, 0, 1, 0#
*/
#define @TYPE@_floor_divide @TYPE@_divide
#define @TYPE@_fmax @TYPE@_maximum
#define @TYPE@_fmin @TYPE@_minimum
NPY_NO_EXPORT void
@TYPE@__ones_like(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(data))
{
OUTPUT_LOOP {
*((@type@ *)op1) = 1;
}
}
NPY_NO_EXPORT void
@TYPE@_positive(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
UNARY_LOOP_FAST(@type@, @type@, *out = +in);
}
/**begin repeat1
* #isa = , _avx2#
* #ISA = , AVX2#
* #CHK = 1, HAVE_ATTRIBUTE_TARGET_AVX2#
* #ATTR = , NPY_GCC_TARGET_AVX2#
*/
#if @CHK@
NPY_NO_EXPORT NPY_GCC_OPT_3 @ATTR@ void
@TYPE@_square@isa@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(data))
{
UNARY_LOOP_FAST(@type@, @type@, *out = in * in);
}
#endif
#if @CHK@
NPY_NO_EXPORT NPY_GCC_OPT_3 @ATTR@ void
@TYPE@_reciprocal@isa@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(data))
{
UNARY_LOOP_FAST(@type@, @type@, *out = 1.0 / in);
}
#endif
#if @CHK@
NPY_NO_EXPORT NPY_GCC_OPT_3 @ATTR@ void
@TYPE@_conjugate@isa@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
UNARY_LOOP_FAST(@type@, @type@, *out = in);
}
#endif
#if @CHK@
NPY_NO_EXPORT NPY_GCC_OPT_3 @ATTR@ void
@TYPE@_negative@isa@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
UNARY_LOOP_FAST(@type@, @type@, *out = -in);
}
#endif
#if @CHK@
NPY_NO_EXPORT NPY_GCC_OPT_3 @ATTR@ void
@TYPE@_logical_not@isa@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
UNARY_LOOP_FAST(@type@, npy_bool, *out = !in);
}
#endif
#if @CHK@
NPY_NO_EXPORT NPY_GCC_OPT_3 @ATTR@ void
@TYPE@_invert@isa@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
UNARY_LOOP_FAST(@type@, @type@, *out = ~in);
}
#endif
/**begin repeat2
* Arithmetic
* #kind = add, subtract, multiply, bitwise_and, bitwise_or, bitwise_xor,
* left_shift, right_shift#
* #OP = +, -,*, &, |, ^, <<, >>#
*/
#if @CHK@
NPY_NO_EXPORT NPY_GCC_OPT_3 @ATTR@ void
@TYPE@_@kind@@isa@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
if(IS_BINARY_REDUCE) {
BINARY_REDUCE_LOOP(@type@) {
io1 @OP@= *(@type@ *)ip2;
}
*((@type@ *)iop1) = io1;
}
else {
BINARY_LOOP_FAST(@type@, @type@, *out = in1 @OP@ in2);
}
}
#endif
/**end repeat2**/
/**begin repeat2
* #kind = equal, not_equal, greater, greater_equal, less, less_equal,
* logical_and, logical_or#
* #OP = ==, !=, >, >=, <, <=, &&, ||#
*/
#if @CHK@
NPY_NO_EXPORT NPY_GCC_OPT_3 @ATTR@ void
@TYPE@_@kind@@isa@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
/*
* gcc vectorization of this is not good (PR60575) but manual integer
* vectorization is too tedious to be worthwhile
*/
BINARY_LOOP_FAST(@type@, npy_bool, *out = in1 @OP@ in2);
}
#endif
/**end repeat2**/
#if @CHK@
NPY_NO_EXPORT NPY_GCC_OPT_3 @ATTR@ void
@TYPE@_logical_xor@isa@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
BINARY_LOOP {
const int t1 = !!*(@type@ *)ip1;
const int t2 = !!*(@type@ *)ip2;
*((npy_bool *)op1) = (t1 != t2);
}
}
#endif
/**end repeat1**/
/**begin repeat1
* #kind = maximum, minimum#
* #OP = >, <#
**/
NPY_NO_EXPORT void
@TYPE@_@kind@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
if (IS_BINARY_REDUCE) {
BINARY_REDUCE_LOOP(@type@) {
const @type@ in2 = *(@type@ *)ip2;
io1 = (io1 @OP@ in2) ? io1 : in2;
}
*((@type@ *)iop1) = io1;
}
else {
BINARY_LOOP {
const @type@ in1 = *(@type@ *)ip1;
const @type@ in2 = *(@type@ *)ip2;
*((@type@ *)op1) = (in1 @OP@ in2) ? in1 : in2;
}
}
}
/**end repeat1**/
NPY_NO_EXPORT void
@TYPE@_power(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func))
{
BINARY_LOOP {
@type@ in1 = *(@type@ *)ip1;
@type@ in2 = *(@type@ *)ip2;
@type@ out;